package service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mybatis.mapper.CommentinfoMapper;
import com.mybatis.po.Commentinfo;

@Service
public class CommentService {
	@Autowired
	private CommentinfoMapper commentinfomapper;
	public int addcomment(Commentinfo commentinfo) {
		return commentinfomapper.addcomment(commentinfo);
	}
	public List<Commentinfo> selectcomment1(String cid){
		List<Commentinfo> all=commentinfomapper.selectcommentstudent(cid);
		Map<String, Commentinfo> map = new HashMap<>();//记录评论//相当于指针
        List<Commentinfo> result = new ArrayList<>();//只是打乱了all数组中的组合，这个过程中没有新new一个commentinfo对象，只是引用了地址
        for (Commentinfo c : all) {
            if (c.getParentid() == null) {
                result.add(c);
            }
            map.put(c.getCommentid(), c);
            
        }
        for (Commentinfo c : all) {
            if (c.getParentid() != null) {
                Commentinfo parent = map.get(c.getParentid());
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(c);
            }
        }
        return result;
	}
	public List<Commentinfo> selectcomment2(String tid){
		List<Commentinfo> all=commentinfomapper.selectcommentteacher(tid);
		Map<String, Commentinfo> map = new HashMap<>();//记录评论//相当于指针
        List<Commentinfo> result = new ArrayList<>();//只是打乱了all数组中的组合，这个过程中没有新new一个commentinfo对象，只是引用了地址
        for (Commentinfo c : all) {
            if (c.getParentid() == null) {
                result.add(c);
            }
            map.put(c.getCommentid(), c);
            
        }
        for (Commentinfo c : all) {
            if (c.getParentid() != null) {
                Commentinfo parent = map.get(c.getParentid());
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(c);
            }
        }
        return result;
	}
}
